<?php
namespace app\index\service;
use think\Db;
class  User{

    //我的咨询
     function userMyquestion(){
         $paginate = 15;
         $join     = [
             ['lt_consult_type c','a.consult_type_id=c.consult_type_id','LEFT'],
         ];
         $user       = session('lt_user');
         $where['a.u_uid'] = $user['u_uid'];
         $order = 'a.ct_addtime desc';
         $info  = Db::name('consult')->alias('a')->join($join)->where($where)->field('a.*,c.ct_name')->order($order)->paginate($paginate);
         $page  = $info->render();
         $data['list'] = $info;
         $data['page'] = $page;
         return $data;
     }


    //我的订单
    function userOrder($state=''){
        $paginate = 15;
        $user       = session('lt_user');
        $where['u_uid'] = $user['u_uid'];
        if($state == 1){
            $where['o_state'] = $state;
        }
        $order = 'o_addtime desc';
        $info  = Db::name('order')->where($where)->field('order_id,o_no,o_type_id,o_money,o_state,o_comment,o_addtime')->order($order)->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }


    //我的服务
    function userCounselorOrder(){
        $paginate = 15;
        $user       = session('lt_user');
        $where['u_uid'] = $user['u_uid'];
        $order = 'co_addtime desc';
        $info  = Db::name('counselor_order')->where($where)->field('co_no,counselor_order_id,co_name,co_state,co_money,co_addtime,co_begintime,co_finishtime')->order($order)->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //我的新闻收藏
    function userNewsCollect(){
        $paginate = 15;
        $user       = session('lt_user');
        $join  = [
            ['lt_news b','a.ucc_id=b.news_id','LEFT'],
            ['lt_news_type c','b.news_type_id=c.news_type_id','LEFT'],
        ];
        $where['a.u_uid']     = $user['u_uid'];
        $where['a.ucc_state'] = 2;
        $order = 'a.ccc_addtime desc';
        $field = 'b.n_title,c.nt_name,a.ccc_addtime,a.ucc_id';
        $info  = Db::name('user_collect')->alias('a')->join($join)->where($where)->field($field)->order($order)->paginate($paginate);
        $page  = $info->render();
        $data['list'] = $info;
        $data['page'] = $page;
        return $data;
    }

    //我的新闻收藏--删除
    function newsDeleteApi($nid){
        if(empty($nid)){
            return messageInfo('101', '系统参数错误!');
        }
        $user       = session('lt_user');
        $where['ucc_id'] = $nid;
        $where['u_uid']  = $user['u_uid'];
        $a = Db::name('user_collect')->where($where)->delete();
        if(!$a){
            return messageInfo('101', '删除失败!');
        }
        return messageInfo('202', '删除成功!');
    }

    //我的密码修改
    function userPasswordAlterApi($password,$new_password,$affirm_password){
        if(empty($password) || empty($new_password) || empty($affirm_password)){
            return messageInfo('101', '旧密码、新密码、确认密码不能为空!');
        }
        if(!preg_match("/^[A-Za-z0-9]+$/",$new_password)){
            return messageInfo('101', '新密码必需为数字、字母组合！');
        }
        if(strlen($new_password) < 6 || strlen($new_password) > 16 ){
            return messageInfo('101', '新密码只允许设置6~16位!');
        }
        if($new_password != $affirm_password){
            return messageInfo('101', '新密码与确认密码不一致!');
        }
        $lt_user       = session('lt_user');
        $user          = Db::name('user');
        $u_password    = $user->where('u_uid',$lt_user['u_uid'])->value('u_password');
        if($u_password != md5($password)){
            return messageInfo('101', '旧密码不正确!');
        }
        $a = $user->where('u_uid',$lt_user['u_uid'])->setField('u_password',md5($new_password));
        if(!$a){
            return messageInfo('101', '修改失败!');
        }
        return messageInfo('202', '修改成功!');
    }

    //用户头像修改
    function userImgApi($file){
        try {
            $move = ROOT_PATH . 'public' . DS . 'uploads/user_img';
            $info =$file->validate(['ext'=>'jpg,png,gif'])->move($move);
            if($info){
                $lt_user       = session('lt_user');
                $url     = config('tempredirect_url').'uploads/user_img/'.$info->getSaveName();
                $a       = Db::name('user')->where('u_uid',$lt_user['u_uid'])->setField('u_img',$url);
                if(!$a){
                    return messageInfo('101', '修改失败!');
                }
                $lt_user['u_img'] = $url;
                session('lt_user',$lt_user);
                return messageInfo('202','修改成功');
            }else{
                // 上传失败获取错误信息
                $error = $file->getError();
                $response = messageInfo('101', $error);
            }
        } catch (\Exception  $e) {
            $response = messageInfo('101','服务器错误');
        }
        return $response;
    }


    //用户修改昵称
    function userNameApi($name){
        if(empty($name)){
            return messageInfo('101', '昵称不能为空!');
        }
        if(mb_strlen($name,'UTF-8') > 6){
            return messageInfo('101', '昵称不能超过6位!');
        }
        $lt_user       = session('lt_user');
        $a       = Db::name('user')->where('u_uid',$lt_user['u_uid'])->setField('u_nickname',$name);
        if(!$a){
            return messageInfo('101', '修改失败!');
        }
        $lt_user['u_nickname'] = $name;
        session('lt_user',$lt_user);
        return messageInfo('202','修改成功');
    }

    //用户修改邮箱
    function userMailApi($mail){
        if(empty($mail)){
            return messageInfo('101', '邮箱不能为空!');
        }
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if(!preg_match( $pattern, $mail )){
            return messageInfo('101', '邮箱格式错误!');
        }
        $lt_user       = session('lt_user');
        $a       = Db::name('user')->where('u_uid',$lt_user['u_uid'])->setField('u_mail',$mail);
        if(!$a){
            return messageInfo('101', '修改失败!');
        }
        $lt_user['u_mail'] = $mail;
        session('lt_user',$lt_user);
        return messageInfo('202','修改成功');
    }



}
